<?php

class My_eventController extends BaseController {
  
    public function indexAction($requestData) {
		$this->data['events'] = p1_event::getAll();
		pk_sport::fillPair($this->data);
		
    	if(isset($requestData['find'])) {
    		BaseController::forward("My_event", "login");
    		My_eventController::loginAction($requestData);
    		return;
    	}
		
    	if(isset($requestData['new_event'])) {
    		BaseController::forward("My_event", "submit");
    		My_eventController::submitAction($requestData);
    		return;
    	}
    	
		// public user already logged
    	if(isset($_SESSION['email'])) {
    		if(isset($requestData['arg0']) && !empty($requestData['arg0'])) {
    			if(!isset($this->data['myevent'])) {
					$this->data['myevent'] = p1_event::get($requestData['arg0']);
					$this->data['myevent']->sports = explode(';', $this->data['myevent']->sports);
    			}
    			$this->data['myevent']->details = p1_event_detail::get($this->data['myevent']->id, 'event');
			} else {
				$myEvents = p1_event::getAll(array('email'=>$_SESSION['email']), array(), 0);
				$this->data['myevents'] = $myEvents;
			}
			if(isset($this->data['myevent']) && $this->data['myevent']->email != $_SESSION['email']) {
				BaseController::redirect('../my_event/');
			}
    	} else {
    		$this->data['public'] = true;
    	}
    }
    
    public function manageAction($requestData) {
    	$this->data['events'] = p1_event::getAll();
    	if(isset($requestData['arg0']) && !empty($requestData['arg0'])) {
    		$this->data['myevent'] = p1_event::get($requestData['arg0']);
    		$search = array('event'=>$this->data['myevent']->id);
    		$this->data['myevent']->photos = p1_event_picture::getAll($search, array('id', 'DESC'));
    		$this->data['myevent']->videos = p1_event_video::getAll($search, array('id', 'DESC'));
    	} else {
    		BaseController::redirect('../my_event/');
    	}
    }
    
    public function deleteAction($requestData) {
    	if(isset($requestData['arg0']) && !empty($requestData['arg0'])) {
    		$event = p1_event::get($requestData['arg0']);
    		$event->delete();
    		
    		BaseController::forward("My_event", "index");
    		My_eventController::indexAction($requestData);
    	}
    }
    
    public function add_photoAction($requestData) {
    	$photo = new p1_event_picture($requestData);
    	$photo->url = $requestData['purl'];  
		if(isset($_FILES['picture']['name']) && !empty($_FILES['picture']['name'])) {
			$photo->url = REAL_ROOT . 'uploads/' . $this->upload('picture');
		}
    	$validation = $photo->validate();
				
		if(is_array($validation)) {
			$this->data['errors'] = $validation;
			$requestData['arg0'] = $requestData['event'];
			$this->data['bodyTpl'] = 'my_event/manage.tpl';
			$this->manageAction($requestData);	
		} else {
			$photo->save();
			BaseController::redirect('../my_event/manage/'. $requestData['event']);
		}
    }
    
    public function add_videoAction($requestData) {
    	$video = new p1_event_video($requestData);
    	$validation = $video->validate();		
		if(is_array($validation)) {
			$this->data['errors'] = $validation;
			$requestData['arg0'] = $requestData['event'];
			$this->data['bodyTpl'] = 'my_event/manage.tpl';
			$this->manageAction($requestData);	
		} else {
			$video->save();
			BaseController::redirect('../my_event/manage/'. $requestData['event']);
		}
		peak100::getDefaults($this->data);
    }
    
    public function delete_videoAction($requestData) {
    	if(isset($requestData['arg1']) && !empty($requestData['arg1'])) {
    		$video = p1_event_video::get($requestData['arg1']);
    		$video->delete();
    	}
    	BaseController::redirect('../my_event/manage/'. $requestData['arg0']);
    }
    
    public function delete_photoAction($requestData) {
    	if(isset($requestData['arg1']) && !empty($requestData['arg1'])) {
    		$photo = p1_event_picture::get($requestData['arg1']);
    		$photo->delete();
    	}
    	BaseController::redirect('../my_event/manage/'. $requestData['arg0']);
    }
    
    public function loginAction($requestData) {
    	$myEvents = p1_event::getAll(array('email'=>$requestData['email']));
    	if(empty($myEvents)) {
    		$this->data['errors1'][] = 'Email does not exists in our database.';
    		unset($requestData['find']);
    		BaseController::forward("My_event", "index");
    		My_eventController::indexAction($requestData);
    		return;
    	} else {
    		$_SESSION['email'] = $requestData['email'];
    		BaseController::redirect('../my_event/');
    	}
    }
    
    public function updateAction($requestData) {
    	$event = p1_event::get($requestData['arg0']);
    	$event->updateFrom($requestData);
    	
    	if(isset($requestData['sports'])) {
    		$event->sports = implode(';', $requestData['sports']);
    	}
    	$event->email = $_SESSION['email'];
		$validation = $event->validate();
		
		if(is_array($validation)) {
			$this->data['errors'] = $validation;	
			if(isset($_FILES['picture']['name']) && !empty($_FILES['picture']['name'])) {
				$event->picture = $this->upload('picture');
				$this->data['myevent'] = $event;
			}
			$this->indexAction($requestData);
		} else {
			if(isset($_FILES['picture']['name']) && !empty($_FILES['picture']['name'])) {
				$event->picture = $this->upload('picture');
			}
			$event->update();
			$requestData['event'] = $event->id;
			$details = new p1_event_detail($requestData);
			$details->update();
			
			if(isset($_SESSION['picture'])) {
				unset($_SESSION['picture']);
			}
			BaseController::redirect('../my_event/');
		}
    }
  
    public function submitAction($requestData) {
    	if(isset($requestData['new_event'])) {
	    	$event = new p1_event($requestData);
	    	$event->rank = p1_event::getNewRank();
	    	if(isset($requestData['sports'])) {
	    		$event->sports = implode(';', $requestData['sports']);
	    	}
			$validation = $event->validate();
			if(!CaptchaController::validateCaptcha($requestData['security_code'])) {
				$message = 'Please enter correct security code.';
				if(is_array($validation))
					$validation[] = $message;
				else $validation = array($message);
			}
			if(is_array($validation)) {
				$this->data['errors2'] = $validation;		
				if(isset($_FILES['picture']['name']) && !empty($_FILES['picture']['name'])) {
					$this->upload('picture', true);
				}
				if(!isset($requestData['standalone'])) {
		    		unset($requestData['new_event']);
		    		BaseController::forward("My_event", "index");
		    		My_eventController::indexAction($requestData);
		    		return;
				}
			} else {
				if(isset($_FILES['picture']['name']) && !empty($_FILES['picture']['name'])) {
					$this->upload('picture', true);
				}
				if(isset($_SESSION['picture'])) {
					$event->picture = $_SESSION['picture'];
					unset($_SESSION['picture']);
				}
				$event->save();
				$requestData['event'] = $event->id;
				$details = new p1_event_detail($requestData);
				$details->save();
				
				$_SESSION['email'] = $requestData['email'];
				BaseController::redirect('../my_event/');
			}
			
    	}
		$this->data['events'] = p1_event::getAll();
		pk_sport::fillPair($this->data);
		
		$this->data['pageTitle'] = 'Submit your event - Peak 100';
		
		$this->data['standalone'] = true;
    }
    
    private function upload($name, $save=false) {
		$picture =  date('mdY') . "_" . $_FILES[$name]['name'];
		if($save != false) {
			$_SESSION[$name] = $picture;
		}
		$target_path = DOC_ROOT . DS. 'webroot' . DS . 'uploads' . DS . $picture;
		@move_uploaded_file($_FILES[$name]['tmp_name'], $target_path);
		return $picture;
    }

}

?>